import FlvJs from 'flv.js';
import Hls from 'hls.js';
import DhFlvVideo from './DhFlvVideo';
import DhHlsVideo from './DhHlsVideo';
import HlsVideo from './HlsVideo';
import FlvVideo from './FlvVideo';
import { WsPropsType } from './WSPlayerVideo';

/**
 * 平台
 * 	大华
 * 		dhhls 264
 * 		dhflv
 * 		wsplayer 264 265
 * 	所托
 * 		原生flv
 */

export enum VideoType {
	flv = 'flv',
	hls = 'hls',
	dhhls = 'dhhls',
	dhflv = 'dhflv',
	wsplayer = 'wsplayer',
}
export type PropsType = {
	[VideoType.flv]: { el: HTMLVideoElement; type: 'flv' | 'mp4'; isLive: boolean; hasAudio: boolean; url: string };
	[VideoType.hls]: { el: HTMLVideoElement; url: string };
	[VideoType.wsplayer]: WsPropsType;
	[VideoType.dhflv]: { el: HTMLVideoElement; type: 'flv' | 'mp4'; isLive: boolean; hasAudio: boolean; url: string };
	[VideoType.dhhls]: { el: HTMLVideoElement; url: string };
};

export default class CommonVideoPlayer {
	private static videoInstanceMap = {
		dhflv: window.dhflvjs.isSupported(),
		dhhls: window.dhhlsjs.isSupported(),
		flv: FlvJs.isSupported(),
		hls: Hls.isSupported(),
		wsplayer: false,
	};

	static VideoType = VideoType;

	/**
	 * 获取视频实列
	 * @param type 视频实列类型
	 * @param props
	 * @param props.type 视频类型 flv类型需要
	 * @param props.isLive 是否直播 flv类型需要
	 * @param props.url 视频连接
	 * @returns
	 */
	static getInstance<T extends VideoType, P extends PropsType[T]>(type: T, props: P) {
		switch (type) {
			case VideoType.dhflv:
				return DhFlvVideo.getInstance(props as PropsType[VideoType.dhflv]);
			case VideoType.dhhls:
				return DhHlsVideo.getInstance(props as PropsType[VideoType.dhhls]);
			case VideoType.flv:
				return FlvVideo.getInstance(props as PropsType[VideoType.flv]);
			case VideoType.hls:
				return HlsVideo.getInstance(props as PropsType[VideoType.hls]);
			case VideoType.wsplayer:
				return;
			default:
				throw new Error(`不支持的播放类型：${type}`);
		}
	}

	static isSupported(type: VideoType): boolean {
		return CommonVideoPlayer.videoInstanceMap[type];
	}
}
